<template>
  <div class="app-container">
    <el-table v-loading="loading" :data="tableData" border style="width: 100%">
      <el-table-column prop="tpl_name" label="模板名称" />
      <el-table-column prop="tpl_trade" label="所属行业" />
      <el-table-column prop="tpl_number" label="模板编号" />
      <el-table-column label="启用状态" width="120">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.is_open" />
        </template>
      </el-table-column>
      <el-table-column prop="tpl_id" label="公众号模板ID" width="600">
        <template slot-scope="scope">
          <el-input v-model="scope.row.tpl_id" />
        </template>
      </el-table-column>
    </el-table>
    <el-button class="save-btn" type="primary" size="medium" @click="saveClick()">保存</el-button>
  </div>
</template>

<script>
import { wechatMassTplList } from '@/api/massSMS'
import { setWechatNotifyRule } from '@/api/configuration'
export default {
  data() {
    return {
      loading: false,
      tableData: []
    }
  },
  methods: {
    initData() {
      this.loading = true
      this.tableData = []
      wechatMassTplList().then(response => {
        if (response.code == 200) {
          const data = [...response.data]
          const that = this
          data.forEach(function(val, index, arr) {
            val.is_open = val.is_open == 1
            that.tableData.push(val)
          })
          that.loading = false
        }
      })
        .catch(() => {})
    },
    saveClick() {
      const insertData = []
      this.tableData.forEach(function(val, index, arr) {
        const tmp_val = {
          id: val.id,
          is_open: val.is_open == -1 ? -1 : val.is_open === true ? 1 : 0,
          tpl_id: val.tpl_id
        }
        insertData[index] = tmp_val
      })
      setWechatNotifyRule(insertData)
        .then(response => {
          this.$message.success(response.message)
          this.initData()
          return true
        })
        .catch(() => {})
    }
  }
}
</script>

<style lang="scss" scoped>
.save-btn {
  margin-top: 20px;
}
</style>
